﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.Config;
using AspNetCs.Common;
using System.Text.RegularExpressions;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Model;
using System.Web;
using System.Data;
using AspNetCs.Enumerations.TrueOrFalseEnum;

namespace AspNetCs.Bll
{

    /// <summary>
    /// 系统静态标签替换与系统缓存信息读取
    /// </summary>
    public class System_Lable
    {
        private static Regex ScriptRx = new Regex(@"(\[\$ScriptMemory\((?<selectnum>([0-9]+))\)\$\])", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);//获取系统标签

        private static Caches GetCaches = new Caches();
        private static string SiteCachePrefix = BaseConfig.GetCachePrefix + StringHelper.ReadConfig("SystemCache");

        /// <summary>
        /// 系统配制信息的缓存
        /// </summary>
        public static SiteConfigInfo SystemGetCache
        {
            get
            {
                if (GetCaches.GetCache(SiteCachePrefix) == null)
                {
                    GetCaches.SetCache(SiteCachePrefix, SiteConfig.GetConfig());
                }
                return (SiteConfigInfo)GetCaches.GetCache(SiteCachePrefix);
            }
        }


        /// <summary>
        /// 清空系统缓存
        /// </summary>
        public static void RefreshSystemCache()
        {
            if (GetCaches.GetCache(SiteCachePrefix) != null)
            {
                GetCaches.ClearCache();
            }
        }



        /// <summary>
        /// 系统静态标签
        /// </summary>
        /// <param name="Context"></param>
        /// <returns></returns>
        public static string GetSysInfo(string Context)
        {
            if (!string.IsNullOrEmpty(Context))
            {
                #region 网站路径

                try
                {
                    Context = Context.Replace("[$WebDir$]", SystemGetCache.InstallDir);
                }
                catch
                {

                }
                #endregion

                #region 网站名称

                try
                {
                    Context = Context.Replace("[$WebName$]", SystemGetCache.SiteName);
                }
                catch
                {

                }
                #endregion

                #region 网站标题

                try
                {
                    Context = Context.Replace("[$WebTitle$]", SystemGetCache.SiteTitle);
                }
                catch
                {

                }
                #endregion

                #region 网站Url地址

                try
                {
                    Context = Context.Replace("[$WebUrl$]", SystemGetCache.SiteUrl);
                }
                catch
                {

                }
                #endregion

                #region 网站Url地址

                try
                {
                    Context = Context.Replace("[$WebUrl$]", SystemGetCache.SiteUrl);
                }
                catch
                {

                }
                #endregion

                #region 网站关键字

                try
                {
                    Context = Context.Replace("[$Keywords$]", SystemGetCache.Meta_Keywords);
                }
                catch
                {

                }
                #endregion
                #region 网站描述

                try
                {
                    Context = Context.Replace("[$Description$]", SystemGetCache.Meta_Description);
                }
                catch
                {

                }
                #endregion
            }
            return Context;


        }







    }

    public class Script_Lable
    {
        private static Regex ScriptRx = new Regex(@"(\[\$ScriptMemory\((?<selectnum>([0-9]+))\,(?<IsScroll>(true|false))\,(?<ScrollH>([0-9]+))\)\$\])", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);//获取系统标签

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Context"></param>
        /// <param name="chetype"></param> 
        /// <param name="_id"></param>
        /// <returns></returns>
        public static string ScriptLable_Replace(string Context, Channel_Type chetype, string _id)
        {
            #region 纪念馆调用最新纪念
            try
            {
                Match mth = ScriptRx.Match(Context);
                string SelectNum = mth.Groups["selectnum"].Value;
                string IsScroll = mth.Groups["IsScroll"].Value;
                string h = mth.Groups["ScrollH"].Value;
                Context = Context.Replace(mth.Value, "<script src='" + System_Lable.SystemGetCache.InstallDir + "Script/ScriptMemory.aspx?id=" + _id + "&channel=" + chetype.ToString() + "&num=" + SelectNum + "&IsScroll=" + IsScroll + "&h=" + h + "'></script>");
            }
            catch
            {

            }
            #endregion
            return Context;
        }
        /// <summary>
        /// js标签的替换
        /// </summary>
        /// <param name="stbing"></param>
        /// <param name="chetype"></param>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static StringBuilder ScriptLable_Replace(StringBuilder stbing, Channel_Type chetype, string _id)
        {
            StringBuilder stb = new StringBuilder();
            stb.AppendLine(ScriptLable_Replace(stbing.ToString(), chetype, _id));
            return stb;
        }




    }

    public class DnsRequest : System.Web.UI.Page
    {
        public DnsRequest()
        {

        }


        /// <summary>
        /// 根据代理商查询出省份，城市 
        /// </summary>
        /// <param name="AgentUserName">代理商用户名</param>
        /// <param name="RequestCity">out 城市</param>
        /// <param name="RequestPro">out 省份</param>
        public static void RequestCityFun(object AgentUserName, out int RequestCity, out int RequestPro)
        {
            RequestPro = 0;
            RequestCity = 0;
            try
            {
                if (AgentUserName != null)
                {
                    if (!string.IsNullOrEmpty(AgentUserName.ToString()))
                    {
                        DataTable dt = ExcuteSqlTextBLL.GetDataTable(string.Format("Select Top 1 LoginPro,LoginCity from Agent Where AgentUserName='{0}'", AgentUserName.ToString()));
                        if (dt.Rows.Count > 0)
                        {

                            RequestCity = StringHelper.isNumber_int(dt.Rows[0][1]);
                            RequestPro = StringHelper.isNumber_int(dt.Rows[0][0]);
                        }
                        else
                        {
                            RequestPro = 0;
                            RequestCity = 0;
                        }
                    }
                    else
                    {
                        RequestPro = 0;
                        RequestCity = 0;


                    }
                }
                else
                {
                    RequestPro = 0;
                    RequestCity = 0;


                }

            }
            catch
            {

                RequestPro = 0;
                RequestCity = 0;
            }
        }
        /// <summary>
        /// 省份，与城市存在时返回的条件
        /// </summary>
        /// <param name="ProColumn">省份列名</param>
        /// <param name="CityColumn">城市列名</param>
        /// <param name="BeginIsAnd">字段串前面的字符</param>
        /// <returns></returns>
        public static string WhereString(string ProColumn, string CityColumn, string BeginIsAnd, int RequestCity, int RequestPro)
        {
            string Result = "";
            if (RequestCity != 0 && RequestPro != 0)
            {
                if (BeginIsAnd != "")
                {
                    Result += BeginIsAnd;
                }
                Result += ("(" + ProColumn + "=" + RequestPro + " and " + CityColumn + "=" + RequestCity + " )");
            }
            return Result;
        }
        /// <summary>
        /// 纪念馆,公墓，宗祠的二级域名的URl地址
        /// </summary>
        /// <param name="Demain"></param>
        /// <param name="Numid"></param>
        /// <param name="ChannelId"></param>
        /// <returns></returns>
        public static string UrlDomainChange(string Demain, string Numid, string ChannelId)
        {

            string Result = "";
            try
            {
                SiteConfigInfo configInfo = System_Lable.SystemGetCache;
                if (Demain == "localhost")
                {


                    Result = configInfo.SiteUrl + "Html/" + ChannelId + "/" + Numid + ".html";

                }
                else
                {
                    if (configInfo.IsDns == 1)
                    {
                        Result = "http://" + Numid + "." + ChannelId + "." + configInfo.DnsUrl;
                    }
                    else
                    {
                        Result = configInfo.SiteUrl + "Html/" + ChannelId + "/" + Numid + ".html";

                    }
                }

            }
            catch
            {
                Result = "/Html/" + ChannelId + "/" + Numid + ".html";


            }

            return Result;
        }
        /// <summary>
        ///  纪念馆,公墓，宗祠的二级域名的URl地址
        /// </summary>
        /// <param name="Numid"></param>
        /// <param name="ChannelId"></param>
        /// <returns></returns>
        public static string UrlDomainChange(string Numid, string ChannelId)
        {
            string dm = HttpContext.Current.Request.Url.Host.ToString();
            return UrlDomainChange(dm, Numid, ChannelId);
        }

        /// <summary>
        /// 根据url获取代理商的城市，省份
        /// </summary>
        /// <returns></returns>
        public static void UrlRequestAgentName(out int RequestCity, out int RequestPro)
        {
            RequestCity = 0;
            RequestPro = 0;
            HttpRequest Request = HttpContext.Current.Request;
            object AgentStr = null;
            try
            {
                if (System_Lable.SystemGetCache.IsDns == (int)IsTrueEnum.Yes)
                {
                    string[] UserHost = Request.Url.Host.Split(new Char[] { '.' });

                    #region 二级域名
                    if (UserHost.Length == 4)
                    {
                        string domain1 = UserHost[0].ToString();
                        string domain2 = UserHost[1].ToString().ToLower();
                        string domain3 = UserHost[2].ToString().ToLower();
                        string domain4 = UserHost[3].ToString().ToLower();
                        if (domain2 == "web" && (domain3 + "." + domain4 == System_Lable.SystemGetCache.DnsUrl.Replace("/", "").ToLower().Trim()))
                        {
                            AgentStr = domain1;
                        }
                    }

                    #endregion
                    RequestCityFun(AgentStr, out RequestCity, out RequestPro);
                }
                else
                {


                }

            }
            catch
            {
                RequestCity = 0;
                RequestPro = 0;
            }

        }


    }

}
